/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.converter;

import com.rbh.risk.entity.DrugItems;
import com.rbh.risk.service.DrugItemsService;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author M
 */
@ManagedBean(name = "drugItemsConvert")
@RequestScoped
@FacesConverter(forClass = DrugItems.class)
public class DrugItemsConverter implements Converter {

    @ManagedProperty("#{drugItemsService}")
    private DrugItemsService drugItemsService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        DrugItems result = null;
        try {
            result = drugItemsService.getById(value);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String result = "";
        try {
            if (value instanceof DrugItems) {
                DrugItems drug = (DrugItems) value;
                result = drug.getIcode();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public DrugItemsService getDrugItemsService() {
        return drugItemsService;
    }

    public void setDrugItemsService(DrugItemsService drugItemsService) {
        this.drugItemsService = drugItemsService;
    }
}